/*
 * TableListener.java
 *
 * Created on April 2, 2007, 3:06 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.gui;

import java.util.ArrayList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Travis Kulbeth
 */
public class TableListener implements ListSelectionListener{
    int prevChangedRow;                                                         //variable defined to contain the row that user navigates away from
    TxLineTableModel activeModel;                                                    //Table_Model to be notified when changes occur
    
    /** Creates a new instance of TableListener */
    public TableListener() {}
    
    /** Creates a new instance of TableListener, importing the Table_Model to be used*/
    public TableListener(TxLineTableModel newModel) {
        prevChangedRow = -1;
        activeModel = newModel;
    }
    
    /**Method that detects if a user has navigated to another row in the table.
     *
     *@param e contains the ListSelectionEvent from the listener
     */
    public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
        
        int firstIndex = e.getFirstIndex();
        int lastIndex = e.getLastIndex();
        boolean isAdjusting = e.getValueIsAdjusting();
//        if (lsm.isSelectionEmpty()) {
//            if(prevChangedRow != -1){                                           //if the user has left a row, but has not selected another row
//                activeModel.updateRow(prevChangedRow);                          //send row number to the model to notify of potential change in that row
//                prevChangedRow = -1;                                            //set the prevChangedRow to -1, since the user is not currently on one
//            }
//        } else {
//            // Find out which indexes are selected.
//            int minIndex = lsm.getMinSelectionIndex();
//            int maxIndex = lsm.getMaxSelectionIndex();
//            if(prevChangedRow != -1){                                           //if the user has left a row and navigated to another one
//                activeModel.updateRow(prevChangedRow);                          //send row number to the model to notify of potential change in that row
//                prevChangedRow = minIndex;                                      //set the prevChangedRow to the currently active row for future reference
//            }else{ prevChangedRow = minIndex; }                                 //if the user was not previously on a row, it doesn't need to send notification of update
//        }
    }
}

